<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <link rel="stylesheet" th:href="@{/js/layuiadmin-1.2.1/style/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/js/layuiadmin-1.2.1/style/dtree/font/dtreefont.css}" media="all">
    <th:block th:include="include :: header('分配数据权限')" />
    <style>
        .west {width:310px;}
        .layui-nav {background: #FFF;color:#000;width:310px;}
        .layui-body {left:310px;}
        .layui-logo{color: #fff;height: 41px;line-height: 41px;font-size: 16px}
        .layui-logo span{display: block;width: 277px;float: left;margin-left: 10px;}

        .layui-side-scroll{border-right: 1px solid #f6f6f6;width:310px;}
        .layui-icon{float: right}
        .layui-icon-shrink-right:before {background-color:rgba(243,160,65,0.9)}
        .layui-icon-spread-left:before {background-color:rgba(243,160,65,0.9)}
    </style>
</head>
<body>
<div class="main-container" id="main-container">
	<div class="main-content">
		<div class="west layui-side">
            <div class="layui-side-scroll" >
                <div class="layui-logo" style="color: #fff;height: 41px;line-height: 41px;">
                    <span >菜单列表</span>
                    <a href="javascript:;" layadmin-event="flexible" class="menuBtn">
                        <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible" style="color: #fff;float: right"></i>
                    </a>
                </div>
                <div class="layui-nav layui-nav-tree">
                    <div id="dhtree" class="demo-tree demo-tree-box"></div>
                </div>
            </div>
            <div class="panel layout-expand layui-expand-west" id="panel" style="display: none; width: 114px; left: 0px; top: 0px;height: 41px;color: #fff;">
                <div class="panel-header">
                    <div class="panel-tool"  id="panel-tool" style="text-align: center; width: 114px;height: 41px;line-height: 41px;font-size: 16px;">
                        <span class="panel-body panel-title" style="width: 114px;">菜单列表</span>
                        <a href="javascript:;" class="menuBtn" >
                            <i class="layui-icon layui-icon-spread-left" id="LAY_app_flexible" style="color: #fff;float: right"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-body" id="LAY_app_body">
        	<div class="mid-content">
				<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
				<p class="f_red" style="font-size:18px;text-align:center;padding-top:10px;">分配权限后，请点击右下角 “确定” 按钮保存哦~</p>
			</div>
		</div>
	</div>
</div>
<th:block th:include="include :: footer" />
<script th:src="@{/dhecp/js/left-tree.js?v=1}"></script>
<script th:src="@{/dhecp/js/selectInTable.js?v=1}"></script>
<script type="text/html" id="select_Org">
	<input type="text" class="layui-input" style="height:28px;padding:0 17px 0 3px" readonly/>
	<i class="layui-icon layui-icon-more getMore" getType="vOrgs"  style="position: absolute;top:0;right: 16px;"></i>			
</script>
<script type="text/html" id="select_GUser">
	<input type="text" class="layui-input" style="height:28px;padding:0 17px 0 3px" readonly/>
	<i class="layui-icon layui-icon-more getMore" getType="vUsers" style="position: absolute;top:0;right: 16px;"></i>			
</script>
<script type="text/html" id="select_limit">
<select name="vDataLimit" lay-filter="limit_select">
	<option></option>
	<div th:each="dict : ${dataLimitMap}" th:utext="'<option value=\''+${dict.key}+'\' {{d.vDataLimit==\''+${dict.key}+'\'?\'selected\':\'\'}}>'+${dict.value}+'</option>'" th:remove="tag"><div>
</select>
</script>
<script th:inline="javascript">
	/*<![CDATA[*/
	var pid = [[${pid}]];
	var vType = [[${vType}]];
	var dataLimitMap=[[${dataLimitMap}]];
	/*]]>*/
</script>
<script type="text/javascript">
    $(".layui-logo").css({"background":pellucidityTen,"text-align":"center"});
    $(".layui-expand-west").css({"background": pellucidityTen,position: "absolute"});
var dataStr = [];setList = ['bList','bView','bAdd','bEdit','bDelete','bCancel','bFlow','bImport','bExport','bPrint','bFile'];
var $,layer,form,table,laydate;
layui.use(['table','form','layer','jquery'], function () {
	$ = layui.$,layer = layui.layer,form = layui.form,table = layui.table,laydate = layui.laydate;
	//行event事件监听
	table.on('tool(ftable)', function(obj){//表格select下拉赋值
		switch(obj.event){
	    	case 'select_limit':
	   			var selectIndex=obj.tr[0].rowIndex;
	   			//监听下拉框
	   			form.on('select(limit_select)', function (obj) {
	   				dataStr[selectIndex]['vDataLimit']=obj.value;
	   			});
	    	break;
    	};
	});
})
$(function(){
	getData("/system/userandrolejurisdiction/list?menuPguid=1");
})

function getData(url){
	$.get(url,function(data,status){
		if(data.count == 0){
			dataStr=[];rend();
		}else{
			dataStr = [];
			for(var i=0;i<data.data.length;i++){
				dataStr.push(data.data[i])
				if(i==data.data.length-1){
					rend();
				}
			}
		}
	});
}

//渲染表格
layui.extend({
        dtree : '/js/layuiadmin-1.2.1/lib/extend/dtree-tp'
    }).use('dtree',function(){
    	var dtree = layui.dtree;
    	CommonTreet = dtree.render({
            elem: "#dhtree",
            data: getTree("/system/menu/listtreejson?menuType=ByModule"),
        });
        dtree.on("node('dhtree')" ,function(obj){
        	var url = '';
        	if(obj.param.level == '1'){
        		url = "/system/userandrolejurisdiction/list?menuPguid="+obj.param.nodeId+"&vType="+vType+"&pid="+pid+"&forData=1"
        	}else if(obj.param.level == '2'){
        		url = "/system/userandrolejurisdiction/list?menuGuid="+obj.param.nodeId+"&vType="+vType+"&pid="+pid+"&forData=1"
        	}else if(obj.param.level == '3'){
        		url = "/system/userandrolejurisdiction/list?searchModule="+obj.param.nodeId+"&vType="+vType+"&pid="+pid+"&forData=1"
        	}
        	getData(url);
        });
    })
function rend(){
	$.tablelist.init({
		elem: '#dhtable'
		,cols: [[
		    {field: 'moduleName', title: '模块名称', width:140, align: 'center'},
		    {field: 'vDataLimit,* ', title: '基础权限', width:150, align: 'center',event:'select_limit' ,templet:'#select_limit'},
		    {field: 'vOrgs', title: '指定机构', width:200, align: 'center', templet:'#select_Org'},
		    {field: 'vUsers', title: '指定人员', width:200, align: 'center', templet:'#select_GUser'},
		    {field: 'vDefinedOr', title: '自定义扩展', width:200, align: 'center', edit: 'text'},
		    {field: 'vDefinedAnd', title: '自定义增强', width:200, align: 'center', edit: 'text'},
		]
		]
		,data:dataStr
		,height:'full-100'
		,done: function(res, curr, count){
			tabledata = res.data;
			//这里是表格重载的时候 回显下拉框的数据
			$('tr').each(function(e){
				var $cr=$(this);
				var dataindex = $cr.attr("data-index");
				$.each(tabledata,function(index,value){
					if(value.LAY_TABLE_INDEX==dataindex){
						$cr.children("[data-field='vUsers']").find('input').val(value.vUsers);
						$cr.children("[data-field='vOrgs']").find('input').val(value.vOrgs);
					}
				});
			});
		}
	});
	$(".getMore").click(function(){
		var obj = $(this), getType = $(this).attr("getType");
        var dataindex = obj.parents('tr').attr('data-index'),
        	dataList = dataStr[dataindex][getType];
        if(getType == 'vUsers'){
        	dataList = dataList?dataList.replace(/\[|]/g,''):'';
        	var mTitle = '选择员工', mUrl = '/system/user/userselect?pagetype=checkbox&selectedUser='+dataList, mWidth = '925', mHeight = '465';
        }else{
        	dataList = dataList?dataList.replace(/\[|]/g,''):'';
        	var mTitle = '选择机构', mUrl = '/system/userandrolejurisdiction/orgselect?vOrgs='+dataList, mWidth = '400', mHeight = '450';
        }
          
        $.modal.open(mTitle, mUrl, mWidth, mHeight, function(index){
            var res = window["layui-layer-iframe" + index].callbackdata();
            if(res){
            	var selectedStr = '';
                $.each(res,function(i,v){
                	if(getType == 'vUsers'){
                		selectedStr += '[' + res[i]['userId'] + '],';
                	}else{
                		selectedStr += '[' + v.recordData.orgPathAll + '],';
                	}
                });
            }
            dataStr[dataindex][getType] = selectedStr.substring(0,selectedStr.length-1);
            rend();
            layer.close(index);
        })
    })
}
var callbackdata = function () {
    return dataStr;
}
</script>
</body>
</html>